def bug_connect(bug_task,user_info_by_realname,type):
    user_bug_counts = {}
    for bug_task_ in bug_task:
        # 检查tasks1中的'finishedBy（完成）'字段是否与tasks2中的'realname'字段匹配
        # print(task_1['finishedBy（完成）'])

        # 如果用户不在字典中，则添加该用户

        # if bug_task_["resolvedBy"] and bug_task_["resolvedBy"] not in user_bug_counts:
        #     user_bug_counts[bug_task_["resolvedBy"]] = {'assignedTo':0,'submitted_bug': 0, 'completed_bug': 0, 'uncompleted_bug': 0,'closed_bug': 0}
        # if bug_task_["assignedTo"] and bug_task_["assignedTo"]!="closed" and bug_task_["assignedTo"] not in user_bug_counts:
        #     user_bug_counts[bug_task_["assignedTo"]] = {'assignedTo':0,'submitted_bug': 0, 'completed_bug': 0, 'uncompleted_bug': 0,'closed_bug': 0}
        if bug_task_["openedBy"] and bug_task_["openedBy"] not in user_bug_counts:
            # user_bug_counts[bug_task_["openedBy"]] = {'assignedTo':0,'submitted_bug': 0, 'completed_bug': 0, 'uncompleted_bug': 0,'closed_bug': 0}
            user_bug_counts[bug_task_["openedBy"]] = {'submitted_bug': 0,'closed_bug': 0}

        if bug_task_["closedBy"] and  bug_task_["closedBy"] not in user_bug_counts:
            # user_bug_counts[bug_task_["closedBy"]] = {'assignedTo':0,'submitted_bug': 0, 'completed_bug': 0, 'uncompleted_bug': 0,'closed_bug': 0}
            user_bug_counts[bug_task_["closedBy"]] = {'submitted_bug': 0,'closed_bug': 0}
        #指派
        # if bug_task_["assignedTo"] and bug_task_["assignedTo"]!="closed" :
        #     user_bug_counts[bug_task_["assignedTo"]]['assignedTo'] += 1

        #已完成  #已完成： 完成人不空
        # if bug_task_["resolvedBy"]:
        #     user_bug_counts[bug_task_["resolvedBy"]]['completed_bug'] += 1
        if bug_task_["closedBy"]:
            user_bug_counts[bug_task_["closedBy"]]['closed_bug'] += 1
        #未完成
        # if bug_task_["assignedTo"] and bug_task_["assignedTo"]!="closed" and not bug_task_["resolvedBy"]:
        #     user_bug_counts[bug_task_["assignedTo"]]['uncompleted_bug'] += 1
        # if bug_task_["openedBy"] and not bug_task_["assignedTo"] and not bug_task_["resolvedBy"]:
        #     user_bug_counts[bug_task_["openedBy"]]['uncompleted_bug'] += 1
        if bug_task_["openedBy"]:
            # print('1',bug_task_["openedBy"])
            # print('2',user_bug_counts)
            user_bug_counts[bug_task_["openedBy"]]['submitted_bug'] += 1

    bug_new_ = []
    for user_,u_d in user_info_by_realname.items():

        if user_ in user_bug_counts:
            dict1 = u_d.copy()
            dict2 = user_bug_counts[user_]
            dict1.update(dict2)
            #print("dict1:",dict1)
        else:
            dict1 = u_d.copy()
            # dict2= {"assignedTo":0,'submitted_bug': 0, 'completed_bug': 0, 'uncompleted_bug': 0,'closed_bug': 0}
            dict2 = {'submitted_bug': 0,'closed_bug': 0}
            dict1.update(dict2)
            #print("dict1:", dict1)
        if type in u_d["name"]:
            bug_new_.append(dict1)
            #print("dict1",dict1)


    return bug_new_

def calculate_closure_rate_scores(bug_new):
    for bug_i in bug_new:
        if bug_i["submitted_bug"] > 0:

           score=(bug_i["closed_bug"]/(bug_i["submitted_bug"]))*100
        else:
           score=0
        bug_i["score"]=score
    return bug_new

def bug_close_percent(bug_task, user_tasks):
    import csv
    # step4 缺陷闭环率 30% 得分 = 当月已闭环的缺陷数/当月已提交的缺陷数 * 100
    # print("——————4缺陷闭环率 30%————————")

    # bug_file = open('bug.csv', 'r')
    # bug_task = csv.DictReader(bug_file)
    # user_file = open('user.csv', 'r')
    # user_tasks = csv.DictReader(user_file)
    user_info_by_realname = {task['account']: task for task in user_tasks}
    type = "测试"
    bug_new = bug_connect(bug_task, user_info_by_realname, "测试")

    scor_ = calculate_closure_rate_scores(bug_new)

    return scor_
    # for i in scor_:
    #     print(i)